package com.momo.service.sysSetting;

import com.momo.common.common.JSONResult;
import com.momo.common.req.sysSetting.authority.AclReq;
import com.momo.common.req.sysmain.RedisUser;
import com.momo.common.util.DateUtil;
import com.momo.common.util.RedisUtil;
import com.momo.common.util.StrUtil;
import com.momo.mapper.dataobject.AclDO;
import com.momo.mapper.mapper.sysSetting.SysAclMapper;
import com.momo.service.async.AclAsync;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * Created by MOMO on 2019/3/21.
 */
@Service
@Slf4j
public class SysAclServiceImpl implements SysAclService {
    @Autowired
    private SysAclMapper sysAclMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private AclAsync aclAsync;

    @Override
    @Transactional
    public JSONResult add(AclReq aclReq, RedisUser redisUser) {
        if (sysAclMapper.checkUrluniqueness(aclReq.getSysAclUrl(), null) > 0) {
            return JSONResult.errorMap("请求的url重复");
        }
        AclDO entity = new AclDO();
        BeanUtils.copyProperties(aclReq, entity);
        entity.setSysAclUrl(aclReq.getSysAclUrl().trim());
        entity.setCreateBy(redisUser.getSysUserName());
        entity.setCreateTime(DateUtil.getDateTime());
        entity.setUpdateBy(redisUser.getSysUserName());
        entity.setUpdateTime(DateUtil.getDateTime());
        entity.setSysAclUuid(StrUtil.genUUID());
        sysAclMapper.insertSelective(entity);
        aclAsync.aclToRedis(entity);
        return JSONResult.ok("新增权限成功");
    }

    @Override
    @Transactional
    public JSONResult modify(AclReq aclReq, RedisUser redisUser) {
        AclDO aclDO = sysAclMapper.uuid(aclReq.getSysAclUuid());
        if (null == aclDO) {
            return JSONResult.errorMap("待编辑的权限点不存在");
        }
        if (sysAclMapper.checkUrluniqueness(aclReq.getSysAclUrl(), aclDO.getId()) > 0) {
            return JSONResult.errorMap("请求的url重复");
        }
        AclDO entity = new AclDO();
        BeanUtils.copyProperties(aclReq, entity);
        entity.setSysAclUrl(aclReq.getSysAclUrl().trim());
        entity.setId(aclDO.getId());
        entity.setUpdateBy(redisUser.getSysUserName());
        entity.setUpdateTime(DateUtil.getDateTime());
        sysAclMapper.updateByPrimaryKeySelective(entity);
        aclAsync.aclToRedis(aclDO);
        return JSONResult.ok("更新权限点成功");
    }

    @Override
    @Transactional
    public JSONResult status(AclReq aclReq, RedisUser redisUser) {
        AclDO aclDO = sysAclMapper.uuid(aclReq.getSysAclUuid());
        if (null == aclDO) {
            return JSONResult.errorMap("待编辑的权限点不存在");
        }
        String statusMsg = "";
        //状态 0启用  1禁用
        if ("0".equals(aclReq.getSysAclStatus())) {
            aclDO.setSysAclStatus("1");
            statusMsg = "权限点状态 禁用 成功";
        } else if ("1".equals(aclReq.getSysAclStatus())) {
            aclDO.setSysAclStatus("0");
            statusMsg = "权限点状态 启用 成功";
        }
        aclDO.setUpdateBy(redisUser.getSysUserName());
        aclDO.setUpdateTime(DateUtil.getDateTime());
        sysAclMapper.updateByPrimaryKeySelective(aclDO);
        aclAsync.aclToRedis(aclDO);
        return JSONResult.ok(statusMsg);
    }

    @Override
    public JSONResult detail(AclReq aclReq) {
        AclDO aclDO=  sysAclMapper.uuid(aclReq.getSysAclUuid());
        if (null == aclDO){
            return JSONResult.errorMap("待查询的权限点不存在");
        }
        return JSONResult.ok(aclDO);
    }
}
